import { Component, OnInit } from '@angular/core';
import { UserDto } from 'src/app/shared/services/http/dto/user.dto';
import { UsersService } from 'src/app/shared/services/http/users.service';
import { CoreService } from 'src/app/shared/services/core.service';

@Component({
  selector: 'app-admin-users',
  templateUrl: './admin-users.component.html',
  styleUrls: ['./admin-users.component.css'],
})
export class AdminUsersComponent implements OnInit {
  // 一些用户数据
  data: UserDto[] = [];

  // 总计
  total = 0;

  // 是否为加载状态
  loading = true;

  // 页大小
  pageSize = 10;

  // 页索引
  pageIndex = 1;

  // 关键字
  keyword = '';

  // 当前页条目数
  currentPageItems = 0;

  constructor(
    private dataService: UsersService,
    private coreService: CoreService
  ) {}

  ngOnInit(): void {
    this.getDataToTable();
  }

  /**
   * 获取一些数据显示到表格
   */
  getDataToTable() {
    this.loading = true;
    this.dataService
      .find({
        pageIndex: this.pageIndex,
        pageSize: this.pageSize,
        keyword: this.keyword,
      })
      .subscribe((res) => {
        this.loading = false;
        this.data = res.data;
        this.total = res.total;
        this.currentPageItems = res.data.length;
      });
  }

  /**
   * 查询参数变化
   * @param params 参数
   */
  onQueryParamsChange() {
    this.getDataToTable();
  }

  /**
   * 确认删除用户
   * @param _id _id
   */
  // tslint:disable-next-line: variable-name
  onConfirm(_id: string) {
    this.dataService.deleteById(_id).subscribe(() => {
      const { pageIndex, currentPageItems } = this.coreService.afterDeletingPag(
        this.pageIndex,
        this.currentPageItems
      );
      this.pageIndex = pageIndex;
      this.currentPageItems = currentPageItems;
      this.getDataToTable();
    });
  }

  /**
   * 单击编辑用户
   * @param item 用户
   */
  onClickEditItem(item) {}

  /**
   * 单击搜索用户
   */
  onClickSearch() {
    this.getDataToTable();
  }
}
